package com.heyun.service.impl;

import com.heyun.entity.dto.BehaviorsLogDTO;
import com.heyun.entity.vo.KeyBehaviorVO;
import com.heyun.entity.vo.UserVO;
import com.heyun.mapper.UserMapper;
import com.heyun.service.UserService;
import com.heyun.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserVO initData() {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String userId = claims.get("id").toString();
        UserVO userVO = userMapper.initData(userId);
        System.out.println(userVO.getRoleId());
        if (userVO.getRoleId() == '2')
            userVO.setClassId(userMapper.getTeaClass(userId));
        else if (userVO.getRoleId() == '1') {
            userVO.setClassId(userMapper.getStuClass(userId));
        }else{
            userVO.setClassId("2109");
        }
        return userVO;
    }

    @Override
    public Boolean updateInfo(String email) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String userId = claims.get("id").toString();
        int result = userMapper.updateInfo(userId,email);
        return result == 1;
    }

    @Override
    public UserVO[] getAll() {
        return userMapper.getAll();
    }

    @Override
    public int BehaviorsLog(BehaviorsLogDTO bl) {
        return userMapper.BehaviorsLog(bl);
    }

    @Override
    public KeyBehaviorVO[] getBehavior(String userId) {
        return userMapper.getBehavior(userId);
    }

    @Override
    @Transactional
    public int updateUserInfo(UserVO userVO) {
        return userMapper.updateUserInfo1(userVO)+userMapper.updateUserInfo2(userVO)+userMapper.updateUserInfo3(userVO);
    }

    @Override
    @Transactional
    public int addUser(UserVO userVO) {
        if (userMapper.addUser(userVO) > 0) {
            return userMapper.addUser1(userVO) + userMapper.addUser2(userVO) + userMapper.addUser3(userVO) + userMapper.addUser4(userVO);
        }
        return 0;
    }
}
